﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BirthdayNote
{
    /// <summary>
    /// 生日会日期选择窗体
    /// 代码编写：黄祯
    /// </summary>
    public partial class BirthPlan : Form
    {
        private DateTime birthday { get; }
        private DateTime today { get; }
        public BirthPlan(DateTime birthday, DateTime today)
        {
            InitializeComponent();

            // 检查生日是否是2月29日
            if (birthday.Month == 2 && birthday.Day == 29)
            {
                DateTime nextLeap = birthday;
                int difLeap = 0;
                // 如果当前年份不是闰年，找到下一个闰年的2月29日
                while (!DateTime.IsLeapYear(nextLeap.Year) || nextLeap < today)
                {
                    nextLeap = nextLeap.AddYears(1);
                    ++difLeap;
                }
                birthday = birthday.AddYears(difLeap);
            }
            else
            {
                // 否则，按照原来的逻辑计算下一个生日
                while (birthday < today)
                {
                    birthday = birthday.AddYears(1);
                }
            }

            this.birthday = birthday;
            this.today = today;

            // 显示下次生日和天数
            nextBirth.Text += birthday.ToString("yyyy年MM月dd日（dddd）");
            dayRemain.Text += (int)(birthday - today).TotalDays + " 天";
        }

        // 选择天数！
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int n = int.Parse(textBox1.Text);  // 提前n天
                DateTime date = birthday.AddDays(-n);
                if (DateTime.Compare(date, today) < 0)
                {
                    MessageBox.Show("预定日期早于今天！请重新输入！");
                }
                //MessageBox.Show(date.ToString("D"));
                else
                {
                    if (date.DayOfWeek != DayOfWeek.Sunday && date.DayOfWeek != DayOfWeek.Saturday)
                    {
                        DateTime newdate;
                        // 计算出最近的周六
                        switch (date.DayOfWeek)
                        {
                            case DayOfWeek.Monday:
                                newdate = date.AddDays(-2);
                                break;
                            case DayOfWeek.Tuesday:
                                newdate = date.AddDays(-3);
                                break;
                            case DayOfWeek.Wednesday:
                                newdate = date.AddDays(3);
                                break;
                            case DayOfWeek.Thursday:
                                newdate = date.AddDays(2);
                                break;
                            case DayOfWeek.Friday:
                                newdate = date.AddDays(1);
                                break;
                            default:
                                newdate = date;
                                break;
                        }
                        ChangeDay change = new ChangeDay(newdate);
                        change.ShowDialog();
                        if (change.DialogResult == DialogResult.Cancel) return;
                        else if (change.DialogResult == DialogResult.Yes) date = newdate;
                    }
                    Results results = new Results(birthday, (int)(birthday - today).TotalDays, n, birthday.AddDays(-n), date);
                    results.Show();
                }
            } catch (Exception) {
                MessageBox.Show("请输入正确的天数！");
            }
        }
    }
}
